package net.hn.hnms.biz.sewage.service;

import net.hn.hnms.biz.sewage.dao.IndustrialWastewaterAmmoniaNitrogenMapper;
import net.hn.hnms.biz.sewage.dao.IndustrialWastewaterMapper;
import net.hn.hnms.biz.sewage.dao.IndustrialWastewaterOdsMapper;
import net.hn.hnms.biz.sewage.dao.SewageIndexMapper;
import net.hn.hnms.biz.sewage.entity.IndustrialWastewaterAmmoniaNitrogenEntity;
import net.hn.hnms.biz.sewage.entity.IndustrialWastewaterOdsEntity;
import net.hn.hnms.biz.sewage.entity.IndustrialWastewaterShiftEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author 智呈物联 nzw
 * @date 2022/9/22
 */
@Service
public class IndustrialWastewaterService {

    private SewageIndexMapper sewageIndexMapper;

    private SewageIndexService sewageIndexService;

    private IndustrialWastewaterMapper industrialWastewaterMapper;

    private IndustrialWastewaterOdsMapper industrialWastewaterOdsMapper;

    private IndustrialWastewaterAmmoniaNitrogenMapper industrialWastewaterAmmoniaNitrogenMapper;

    @Autowired
    public void setSewageIndexService(SewageIndexService sewageIndexService) {
        this.sewageIndexService = sewageIndexService;
    }

    @Autowired
    public void setIndustrialWastewaterAmmoniaNitrogenMapper(IndustrialWastewaterAmmoniaNitrogenMapper industrialWastewaterAmmoniaNitrogenMapper) {
        this.industrialWastewaterAmmoniaNitrogenMapper = industrialWastewaterAmmoniaNitrogenMapper;
    }

    @Autowired
    public void setIndustrialWastewaterOdsMapper(IndustrialWastewaterOdsMapper industrialWastewaterOdsMapper) {
        this.industrialWastewaterOdsMapper = industrialWastewaterOdsMapper;
    }

    @Autowired
    public void setIndustrialWastewaterMapper(IndustrialWastewaterMapper industrialWastewaterMapper) {
        this.industrialWastewaterMapper = industrialWastewaterMapper;
    }

    @Autowired
    public void setSewageIndexMapper(SewageIndexMapper sewageIndexMapper) {
        this.sewageIndexMapper = sewageIndexMapper;
    }

    public List<IndustrialWastewaterShiftEntity> List(Long indexId){
        List<IndustrialWastewaterShiftEntity> list = industrialWastewaterMapper.findAllByIndexId(indexId);
        for (IndustrialWastewaterShiftEntity industrialWastewaterShiftEntity:list){
            industrialWastewaterShiftEntity.setIndustrialWastewaterOdsEntityList(industrialWastewaterOdsMapper.findAllByShiftId(industrialWastewaterShiftEntity.getId()));
            industrialWastewaterShiftEntity.setIndustrialWastewaterAmmoniaNitrogenEntityList(industrialWastewaterAmmoniaNitrogenMapper.findAllByShiftId(industrialWastewaterShiftEntity.getId()));
        }
        return list;
    }
    @Transactional
    public void save(IndustrialWastewaterShiftEntity industrialWastewaterShiftEntity){
        Long indexId = industrialWastewaterShiftEntity.getIndexId();
//        if (indexId!=null){
//            List<IndustrialWastewaterShiftEntity> list = industrialWastewaterMapper.findAllByIndexId(indexId);
//            Long totalSewageTreatment = 0l;
//            Long totalOverflow = 0l;
//            Long totalReuse = 0l;
//            Long totalCoolingSprinkler = 0l;
//            Long totalDownhole = 0l;
//            Long totalRaisedPool = 0l;
//            Long totalGroutingStation = 0l;
//            for (IndustrialWastewaterShiftEntity d:list){
//                if (d.getId()==industrialWastewaterShiftEntity.getId()){
//                    continue;
//                }
////                totalSewageTreatment += Long.parseLong(d.getSewageTreatment());
////                totalOverflow += Long.parseLong(d.getOverflow());
////                totalReuse += Long.parseLong(d.getReuse());
////                totalCoolingSprinkler += Long.parseLong(d.getCoolingSprinkler());
////                totalDownhole += Long.parseLong(d.getDownhole());
////                totalRaisedPool += Long.parseLong(d.getRaisedPool());
////                totalGroutingStation += Long.parseLong(d.getGroutingStation());
//
//            }
////            totalSewageTreatment += Long.parseLong(industrialWastewaterShiftEntity.getSewageTreatment());
////            totalOverflow += Long.parseLong(industrialWastewaterShiftEntity.getOverflow());
////            totalReuse += Long.parseLong(industrialWastewaterShiftEntity.getReuse());
////            totalCoolingSprinkler += Long.parseLong(industrialWastewaterShiftEntity.getCoolingSprinkler());
////            totalDownhole += Long.parseLong(industrialWastewaterShiftEntity.getDownhole());
////            totalRaisedPool += Long.parseLong(industrialWastewaterShiftEntity.getRaisedPool());
////            totalGroutingStation += Long.parseLong(industrialWastewaterShiftEntity.getGroutingStation());
//            for (IndustrialWastewaterShiftEntity d:list){
//                if (d.getId()==industrialWastewaterShiftEntity.getId()){
//                    continue;
//                }
//                d.setTotalSewageTreatment(totalSewageTreatment+"");
//                d.setTotalOverflow(totalOverflow+"");
////                d.setTotalReuse(totalReuse+"");
//                d.setTotalCoolingSprinkler(totalCoolingSprinkler+"");
//                d.setTotalDownhole(totalDownhole+"");
//                d.setTotalRaisedPool(totalRaisedPool+"");
//                d.setTotalGroutingStation(totalGroutingStation+"");
//                industrialWastewaterMapper.save(d);
//            }
//            industrialWastewaterShiftEntity.setTotalSewageTreatment(totalSewageTreatment+"");
//            industrialWastewaterShiftEntity.setTotalOverflow(totalOverflow+"");
////                d.setTotalReuse(totalReuse+"");
//            industrialWastewaterShiftEntity.setTotalCoolingSprinkler(totalCoolingSprinkler+"");
//            industrialWastewaterShiftEntity.setTotalDownhole(totalDownhole+"");
//            industrialWastewaterShiftEntity.setTotalRaisedPool(totalRaisedPool+"");
//            industrialWastewaterShiftEntity.setTotalGroutingStation(totalGroutingStation+"");
//        }

        List<IndustrialWastewaterOdsEntity> odsList = industrialWastewaterShiftEntity.getIndustrialWastewaterOdsEntityList();
        List<IndustrialWastewaterAmmoniaNitrogenEntity> ammList = industrialWastewaterShiftEntity.getIndustrialWastewaterAmmoniaNitrogenEntityList();
        //设置值班人
//        List<Map<String,String>> users = industrialWastewaterShiftEntity.getUser();
//        Map<String,String> map = PersonOnDuty.user(users);
//        industrialWastewaterShiftEntity.setPersonOnDuty(map.get("userName"));
//        industrialWastewaterShiftEntity.setPersonOnDutyCode(map.get("userCode"));
        industrialWastewaterShiftEntity = industrialWastewaterMapper.save(industrialWastewaterShiftEntity);

        //更新总体表
        sewageIndexService.updateIndustriaWastewater(industrialWastewaterShiftEntity);

        Long shiftId = industrialWastewaterShiftEntity.getId();
        List<IndustrialWastewaterOdsEntity> odsAllByShiftId = industrialWastewaterOdsMapper.findAllByShiftId(shiftId);
        industrialWastewaterOdsMapper.deleteAll(odsAllByShiftId);
        if (odsList!=null&&!odsList.isEmpty()) {
            for (IndustrialWastewaterOdsEntity ino : odsList) {
                ino.setShiftId(shiftId);
                industrialWastewaterOdsMapper.save(ino);
            }
        }
        List<IndustrialWastewaterAmmoniaNitrogenEntity> allByShiftId = industrialWastewaterAmmoniaNitrogenMapper.findAllByShiftId(shiftId);
        industrialWastewaterAmmoniaNitrogenMapper.deleteAll(allByShiftId);
        if (ammList!=null&&!ammList.isEmpty()) {
            for (IndustrialWastewaterAmmoniaNitrogenEntity amm : ammList) {
                amm.setShiftId(shiftId);
                industrialWastewaterAmmoniaNitrogenMapper.save(amm);
            }
        }

    }

    public void delete(Long id){
        sewageIndexMapper.deleteById(id);
    }
}
